package test.serverframe.armc.server.manager.controller;

import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import test.serverframe.armc.server.manager.common.ResultDtoUtil;
import test.serverframe.armc.server.manager.common.exception.ExceptionHandle;
import test.serverframe.armc.server.manager.domain.Tag;
import test.serverframe.armc.server.manager.domain.TaskInvoke;
import test.serverframe.armc.server.manager.domain.params.TagPageParams;
import test.serverframe.armc.server.manager.dto.ResultDto;
import test.serverframe.armc.server.manager.service.impl.TagServiceImpl;

import javax.validation.Valid;
import java.util.List;

/**
 * @Author: Uaena
 * @Date: 2018/12/29 0029 16:44
 * @Version 1.0
 */
@CrossOrigin(origins = "*")
@Api("标签管理")
@Validated
@RequestMapping("/tags")
@RestController
public class TagsController {
    private Logger logger = LoggerFactory.getLogger(TagsController.class);
    @Autowired
    private TagServiceImpl tagService;

    /**
     * 标签分页自定义异常
     */
    @Autowired
    private ExceptionHandle<PageInfo<Tag>> exceptionHandle;

    /**
     * Author UaenaSA
     * @param taskInvokeId 调度ID
     * @return 当前调度的标签集合
     */
    @GetMapping("/findTags/{taskInvokeId}")
    @ApiOperation(notes = "获取当前调度的标签集合", value = "获取当前调度的标签集合", httpMethod = "GET")
    public ResultDto<List<Tag>> findTags(@ApiParam @PathVariable @Valid String taskInvokeId) {
        ResultDto<List<Tag>> result = null;
        try {
            List<Tag> tags = tagService.findTagsByInvokeId(taskInvokeId);
            result = ResultDtoUtil.success(tags);
        } catch (Exception e) {
            result = ResultDtoUtil.error(500, "获取当前调度的标签集合失败");
        }

        return result;

    }

    /**
     * Author UaenaSA
     * @param tagId 标签ID
     * @return 当前标签的调度集合
     */
    @GetMapping("/findInvokes/{tagId}")
    @ApiOperation(notes = "获取当前标签的调度集合", value = "获取当前标签的调度集合", httpMethod = "GET")
    public ResultDto<List<TaskInvoke>> findInvokes(@ApiParam @PathVariable @Valid String tagId) {
        ResultDto<List<TaskInvoke>> result = null;
        try {
            List<TaskInvoke> invokes = tagService.findInvokesByTagId(tagId);
            result = ResultDtoUtil.success(invokes);
        } catch (Exception e) {
            result = ResultDtoUtil.error(500, "获取当前标签的调度集合失败");
        }

        return result;

    }

    /**
     * @Author UaenaSA
     * @param tagId 标签ID
     * @return 成功条数
     */
    @DeleteMapping("/delete/{tagId}")
    @ApiOperation(notes = "根据标签ID删除此标签", value = "根据标签ID删除此标签")
    public ResultDto<Integer> deleteTag(@ApiParam @PathVariable @Valid String tagId) {
        ResultDto<Integer> result = null;
        try {
            result = ResultDtoUtil.success(tagService.deleteTag(tagId));
        } catch (Exception e) {
            result = ResultDtoUtil.error(500, "根据标签ID删除此标签失败");
        }

        return result;

    }

    /**
     * @Author UaenaSA
     * @param tag 标签实体
     * @return 成功条数
     */
    @PutMapping("/modify")
    @ApiOperation(notes = "修改标签", value = "修改标签")
    public ResultDto<Integer> updateTask(@ApiParam("标签实体") @RequestBody @Valid Tag tag) {
        ResultDto<Integer> result;
        try {
            result = ResultDtoUtil.success(tagService.updataTag(tag));
        } catch (Exception ex) {
            ex.printStackTrace();
            result = ResultDtoUtil.error(500, "修改标签错误");
            logger.error("修改标签错误", ex);

        }
        return result;
    }

    /**
     * @Author UaenaSA
     * @param tag 标签实体
     * @return 成功条数
     */
    @PostMapping("/add")
    @ApiOperation(notes = "增加标签", value = "增加标签")
    public ResultDto<Integer> addTask(@ApiParam("标签实体") @RequestBody @Valid Tag tag) {
        ResultDto<Integer> result;
        try {
            result = ResultDtoUtil.success(tagService.addTag(tag));
        } catch (Exception ex) {
            ex.printStackTrace();
            result = ResultDtoUtil.error(500, "增加标签错误");
            logger.error("增加标签错误", ex);

        }
        return result;
    }

    /**
     * @Author UaenaSA
     * @param ids 标签ID集合
     * @return
     */
    @DeleteMapping("/delete")
    @ApiOperation(notes = "批量删除标签", value = "批量删除标签")
    public ResultDto<Integer> deleteTask(@RequestBody List<String> ids) {
        ResultDto<Integer> result;
        try {
            ids.forEach(id -> {
                tagService.deleteTag(id);
            });
            result = ResultDtoUtil.success();
        } catch (Exception ex) {
            result = ResultDtoUtil.error(500, "批量删除标签错误");
        }
        return result;
    }

    /**
     * @Author UaenaSA
     * @param params 标签分页查询参数
     * @return 标签分页结果
     */
    @GetMapping("/page")
    @ApiOperation(notes = "标签分页查询", value = "标签分页查询",httpMethod = "GET")
    public ResultDto<PageInfo<Tag>> tagPages(@ModelAttribute @Valid TagPageParams params) {
        ResultDto<PageInfo<Tag>> result;
        try {
            PageInfo<Tag> mbrValues = tagService.pageFind(params.getPageNum(), params.getPageSize(), params);
            result = ResultDtoUtil.success(mbrValues);
        } catch (Exception ex) {
            result = exceptionHandle.exceptionGet(ex);
            logger.error("标签分页查询", ex);
        }
        return result;
    }


}
